import Https from "@/utils/Https";
import { ActionType, ProTable } from "@ant-design/pro-components";
import React, { useState, useRef, FC } from "react";
import { Drawer } from "antd";
import { calcColumns2 } from "./interface";
const MessagePush: FC<any> = () => {
  const [loading, setLoading] = useState(false);
  const actionRef = useRef<ActionType>();
  const [open, setOpen] = useState(false);
  const [messageId, setMessageId] = useState("");
  //推送记录列表
  const columns = [
    {
      title: "消息分类",
      dataIndex: "messageCategory",
      valueEnum: {
        1: { text: "平台消息" },
        2: { text: "服务消息" },
        3: { text: "交易消息" },
      },
    },
    {
      title: "消息标题",
      dataIndex: "messageTitle",
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: "接受人数",
      dataIndex: "userCount",
      hideInSearch: true,
      render: (_, record) => {
        return record.userCount > 0 ? (
          <a
            onClick={() => {
              setOpen(true);
              setMessageId(record.id);
            }}
          >
            {record.userCount}
          </a>
        ) : (
          record.userCount
        );
      },
    },
    {
      title: "消息内容",
      dataIndex: "messageContent",
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: "发送时间",
      dataIndex: "remindTime",
      valueType: "dateRange",
      search: {
        transform: (value: any[]) => {
          return { startTime: value[0], endTime: value[1] };
        },
      },
      render: (_, record) => (record.remindTime ? record.remindTime : ""),
    },
  ];
  //获取推送记录列表
  const getList = async (obj) => {
    setLoading(true);
    let records = [];
    let total = 0;
    let res = await Https.get("/message/center/messageSetList", obj, true);
    setLoading(false);
    if (res.code == 200) {
      records = res.data.records;
      total = res.data.total;
    }
    return {
      success: true,
      data: records,
      total: total,
    };
  };
  //获取接受人数列表
  const getUserList = async (obj) => {
    let params = {
      ...obj,
      id: messageId,
    };
    setLoading(true);
    let records = [];
    let total = 0;
    let res = await Https.get("/message/center/userPageList", params, true);
    setLoading(false);
    if (res.code == 200) {
      records = res.data.records;
      total = res.data.total;
    }
    return {
      success: true,
      data: records,
      total: total,
    };
  };
  return (
    <div key="Course">
      <ProTable
        columns={columns}
        rowKey="id"
        actionRef={actionRef}
        loading={loading}
        pagination={{
          showSizeChanger: true,
        }}
        search={{span:12}}
        request={async (params) => {
          return await getList(params);
        }}
      />
      <Drawer
        open={open}
        title="接受人数"
        forceRender
        onClose={() => setOpen(false)}
        width="60%"
      >
        {open && (
          <ProTable
            search={{span:12}}
            options={false}
            columns={calcColumns2}
            rowKey="userId"
            actionRef={actionRef}
            loading={loading}
            pagination={{
              showSizeChanger: true,
            }}
            request={async (params) => {
              return await getUserList(params);
            }}
          />
        )}
      </Drawer>
    </div>
  );
};
export default MessagePush;
